package com.xiaoniu.douyinvideo.util

import android.text.TextUtils
import com.xiaoniu.douyinvideo.apps.GifMakerApp
import com.xiaoniu.douyinvideo.apps.TikTokVideo
import com.xiaoniu.douyinvideo.apps.VideoApp
import java.util.regex.Pattern

/**
 * @author zhaohe
 * @date 2021/3/26 14:20
 * @desc
 */
object VideoAppUtil {
    /**
     * 判断是否支持
     *
     * @param clipText
     * @return
     */
    @JvmStatic
    fun isSupportApp(clipText: String?): VideoApp? {
        if (TextUtils.isEmpty(clipText)) {
            return null
        }

        val url = VideoAppUtil.findUrlByStr(clipText!!)
        if (url.contains("douyin")) {
            return TikTokVideo(url)
        } else if (url.contains("https://v.kuaishou.com")) {
            return GifMakerApp(url)
        }
        return null
    }


    /**
     * 找出url
     *
     * @param data
     * @return
     */
    fun findUrlByStr(data: String): String {
        val pattern = Pattern.compile(
            "https?://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]",
            Pattern.CASE_INSENSITIVE
        )
        val matcher = pattern.matcher(data)
        if (matcher.find()) {
            return matcher.group()
        }
        return ""
    }
}
